package com.rr.mymoney.upload.parser.excel;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.rr.mymoney.upload.ImportData;
import com.rr.mymoney.upload.Item;
import com.rr.mymoney.upload.parser.BankAccountTransactionsParser;

public class IngRomaniaExtrasDeContExcelParser_from_may_2013 implements BankAccountTransactionsParser {

    private static transient Logger log = Logger.getLogger(IngRomaniaExtrasDeContExcelParser_from_may_2013.class);

    public SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMMM yyyy", new Locale("RO", "ro"));
    public NumberFormat numberFormat = NumberFormat.getInstance(new Locale("RO", "ro"));

    public boolean test(Workbook workbook) {
	//
	// Get the first sheet on the workbook.
	//

	try {
	    Sheet sheet = workbook.getSheetAt(0);

	    //
	    // When we have a sheet object in hand we can iterator on
	    // each sheet's rows and on each row's cells. We store the
	    // data read on an ArrayList so that we can printed the
	    // content of the excel to the console.
	    //

	    Row row = (Row) sheet.getRow(0);

	    if (!"Data".equals(row.getCell(2).getStringCellValue().trim())
		    || !"Detalii tranzactie".equals(row.getCell(4).getStringCellValue().trim())
		    || !"Debit".equals(row.getCell(6).getStringCellValue().trim())
		    || !"Credit".equals(row.getCell(8).getStringCellValue().trim())) {
		return false;
	    }
	} catch (Exception e) {
	    log.debug("Error in test:", e);
	    return false;
	}
	return true;
    }

    public ImportData parse(Workbook workbook) throws ParseException {

	ImportData importFile = new ImportData();
	// Get the first sheet on the workbook.
	Sheet sheet = workbook.getSheetAt(0);

	// When we have a sheet object in hand we can iterator on each sheet's
	// rows and on each row's cells. We store the data read on an ArrayList
	// so that we can printed the content of the excel to the console.

	Row row = sheet.getRow(1);

	Item item = new Item();
	Iterator<Row> rows = sheet.rowIterator();
	while (rows.hasNext()) {
	    row = rows.next();

	    Cell cell = row.getCell(1);
	    String value = cell.getStringCellValue();
	    if (value != null && value.trim().length() > 0) {

		if ("Data".equals(value)) {

		    // do nothing - go to next line

		} else if ("Sold initial:".equals(value)) {

		    cell = (Cell) row.getCell(3);
		    String ib = cell.getStringCellValue();
		    Number ibNumber = numberFormat.parse(ib);
		    importFile.setInitialBalance(ibNumber.floatValue());

		} else if ("Sold final:".equals(value)) {

		    cell = (Cell) row.getCell(3);
		    String fb = cell.getStringCellValue();
		    Number fbNumber = numberFormat.parse(fb);
		    importFile.setFinalBalance(fbNumber.floatValue());

		} else {
		    Date date = dateFormat.parse(value);
		    // if the date is ok create a new item
		    item = new Item();
		    importFile.getItems().add(item);

		    item.setDate(date);

		    // set details
		    cell = (Cell) row.getCell(3);
		    String details = cell.getStringCellValue();
		    if (details != null) {
			item.setDetails((item.getDetails() != null ? item.getDetails() : "") + details + " ");
		    }

		    // set value
		    cell = (Cell) row.getCell(5);
		    String debit = cell.getStringCellValue();
		    if (debit != null && debit.trim().length() > 0) {
			Number debitNumber = numberFormat.parse(debit);
			item.setValue(0 - debitNumber.floatValue());
		    }

		    cell = (Cell) row.getCell(7);
		    String credit = cell.getStringCellValue();
		    if (credit != null && credit.trim().length() > 0) {
			Number debitNumber = numberFormat.parse(credit);
			item.setValue(debitNumber.floatValue());
		    }
		}
	    } else {
		cell = (Cell) row.getCell(3);
		String details = cell.getStringCellValue();
		if (details != null) {
		    item.setDetails((item.getDetails() != null ? item.getDetails() : "") + details + " ");
		}
	    }
	}
	return importFile;
    }
}
